﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.ServiceModel.Syndication;
using System.Net.Mime;

namespace dasBlog.Storage
{
    
    [DataContract()]
    public class TextEntry : IEntry<TextEntry>
    {
        [DataMember(EmitDefaultValue = false)]
        public virtual DateTime Created { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual DateTime LastChange { get; set; }
        [DataMember(EmitDefaultValue = true, IsRequired = true)]
        public virtual Moniker Id { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual string Parent { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual List<string> Categories { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual string Title { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual string CompressedTitle { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual string Reference { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public virtual AuthorDescription Author { get; set; }
        [DataMember(EmitDefaultValue=false)]
        public string Language { get; set; }
        [DataMember(EmitDefaultValue=false)]
        public string Content { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public string Summary { get; set; }
        [DataMember(EmitDefaultValue=false)]
        public string ContentType { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public bool? Published { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public bool? Syndicated { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public bool? Featured { get; set; }
        [DataMember(EmitDefaultValue = false)]
        public bool? AllowComments { get; set; }
                

        public TextEntry()
        {
            Created = LastChange = DateTime.UtcNow;
            Author = new AuthorDescription();
            Categories = new List<string>();
        }

        public SyndicationItem ToSyndicationItem<T>()
        {
            var item = new SyndicationItem();
            if (string.IsNullOrEmpty(Summary))
            {
                item.Summary = SyndicationContent.CreatePlaintextContent(Content);
                item.Content = SyndicationContent.CreateHtmlContent(Content);
            }
            else
            {
                item.Summary = SyndicationContent.CreatePlaintextContent(Summary);
                item.Content = SyndicationContent.CreateHtmlContent(Content);
            }
            item.Title = SyndicationContent.CreatePlaintextContent(Title);
            item.LastUpdatedTime = LastChange;
            item.PublishDate = Created;
            item.Links.Add( SyndicationLink.CreateSelfLink(Id.ToUri()));
            item.Authors.Add(new SyndicationPerson(Author.Email, Author.DisplayName, ""));
            item.ElementExtensions.Add(SerializationTools<TextEntry>.ToXmlElement(this));
            foreach( SyndicationCategory cat in from string s in this.Categories select new SyndicationCategory(s))
            {
                item.Categories.Add(cat);
            }
            item.Id = Id.ToString();
            return item;
        }

        public string CategoriesAsString
        {
            get
            {
                StringBuilder tags = new StringBuilder();
                foreach (string s in Categories)
                {
                    if (tags.Length > 0) tags.Append(';');
                    tags.Append(s);
                }
                return tags.ToString();
            }
            set
            {
                Categories = new List<string>();
                if (value != null)
                {
                    var list = new List<string>(value.Split(';'));
                    foreach (string s in list)
                    {
                        string tag = s.Trim();
                        if (!string.IsNullOrEmpty(tag))
                        {
                            Categories.Add(tag);
                        }
                    }
                }
            }
        }



        #region IExtensibleDataObject Members

        public ExtensionDataObject ExtensionData
        {
            get;
            set;
        }

        #endregion

        public bool IsAuthorized(System.IdentityModel.Claims.ClaimSet claim)
        {
            return true;
        }

    }
}
